package com.jyb.util.capture;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSON;
import com.jyb.config.HttpCilentConfig;
import com.jyb.util.UrlUtil;
import com.jyb.vo.CbxxVo;
import com.jyb.vo.GkgVo;
import com.jyb.vo.GycwVo;

public class CbxxUtil {

	public static List<CbxxVo> capture(String cm){
		
		List<CbxxVo> list=new ArrayList<CbxxVo>();
		
		CloseableHttpClient httpclient = HttpClients.createDefault();
		CloseableHttpResponse response=null;
		try {
			
			String url="http://edi.easipass.com/dataportal/q.do?qn=dp_export_shipinfo";
			url=UrlUtil.hanadleUrl(url);
			
			HttpGet httpGet = HttpCilentConfig.getHttpGet(url);
			
			response = httpclient.execute(httpGet);
			HttpEntity entity = response.getEntity();
			String str=EntityUtils.toString(entity);
			Document document=Jsoup.parse(str);
			String qid=document.select("input[name=qid]").get(0).val();
			url="http://edi.easipass.com/dataportal/query.do?vessel1="+cm+"&vess_call1=&voyage1=&carr_code1=&ship_agen1=&berth1=&esti_sail1=&esti_sail2=&qid="+qid+"&pagesize=30";
			url=UrlUtil.hanadleUrl(url);
			HttpClientUtils.closeQuietly(response);
			
			httpGet = HttpCilentConfig.getHttpGet(url);
			response = httpclient.execute(httpGet);
			entity = response.getEntity();
			str=EntityUtils.toString(entity);
			document=Jsoup.parse(str);
			
			Elements elements=document.select("table[bordercolor=#7996c1][bgcolor=#FFFFFF]").get(0).select("tr:gt(0)");
			for (Element element : elements) {
				Elements elementTds=element.select("td");
				CbxxVo cbxxVo=new CbxxVo();
				cbxxVo.setCm(elementTds.get(0).text());
				cbxxVo.setHh(elementTds.get(1).text());
				cbxxVo.setHc(elementTds.get(2).text());
				cbxxVo.setCyr(elementTds.get(3).text());
				cbxxVo.setDl(elementTds.get(4).text());
				cbxxVo.setMt(elementTds.get(5).text());
				cbxxVo.setYjlg(elementTds.get(6).text());
				cbxxVo.setIMOBH(elementTds.get(7).text());
				
				try {
					
					List<GkgVo> gkgVos=new ArrayList<GkgVo>();
					List<GycwVo> gycwVos=new ArrayList<GycwVo>();
					
					String[] urls=new String[2];
					urls[0]="http://edi.easipass.com/dataportal/"+elementTds.get(8).select("a").get(0).attr("href");//挂靠港
					urls[0]=UrlUtil.hanadleUrl(urls[0]);
					urls[1]="http://edi.easipass.com/dataportal/"+elementTds.get(8).select("a").get(1).attr("href");//共用仓位
					urls[1]=UrlUtil.hanadleUrl(urls[1]);
					
					httpGet = HttpCilentConfig.getHttpGet(urls[0]);
					response = httpclient.execute(httpGet);
					entity = response.getEntity();
					str=EntityUtils.toString(entity);
					Document document2=Jsoup.parse(str);
					Elements elements2=document2.select("table[bordercolor=#7996c1][bgcolor=#FFFFFF]").get(0).select("tr:gt(0)");
					for (Element element2 : elements2) {
						GkgVo gkgVo=new GkgVo();
						Elements elementTd2s=element2.select("td");
						gkgVo.setGkg(elementTd2s.get(0).text());
						gkgVo.setGkgdm(elementTd2s.get(1).text());
						gkgVos.add(gkgVo);
						
					}
					cbxxVo.setGkgs(gkgVos);
					
					HttpClientUtils.closeQuietly(response);
					
					httpGet = HttpCilentConfig.getHttpGet(urls[1]);
					response = httpclient.execute(httpGet);
					entity = response.getEntity();
					str=EntityUtils.toString(entity);
					Document document3=Jsoup.parse(str);
					Elements elements3=document3.select("table[bordercolor=#7996c1][bgcolor=#FFFFFF]").get(0).select("tr:gt(0)");
					for (Element element3 : elements3) {
						GycwVo gycwVo=new GycwVo();
						Elements elementTd3s=element3.select("td");
						gycwVo.setEDIDM(elementTd3s.get(0).text());
						gycwVo.setZwmc(elementTd3s.get(1).text());
						gycwVos.add(gycwVo);
					}
					cbxxVo.setGycws(gycwVos);
					
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				list.add(cbxxVo);
			}
			
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(response!=null){
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		System.out.println(JSON.toJSONString(list));
		return list;
	}
	
	public static void main(String[] args) {
		//CbxxUtil.capture("BRIGHT%20LAEM%20CHABANG");
		CbxxUtil.capture("BRIGHT LAEM CHABANG");
	}
}
